﻿// See https://aka.ms/new-console-template for more information
using CommonLib;
using ConsoleAppConsumer2;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;

Console.WriteLine("AppConsumer2已启动");

#region 工作模式
//// 队列名称
//var queueName = "workQueues";
//// 获取channel
//var channel = RabbitMQUtil.CreateChannel();
//Console.WriteLine(channel.IsOpen);
//// 回调
//var consumer = new AsyncEventingBasicConsumer(channel);
//consumer.Received += Consumer_Registered;
//// 消费消息
//channel.BasicConsume(queueName, false, consumer);
//Console.ReadLine();
//async Task Consumer_Registered(object sender, BasicDeliverEventArgs @event)
//{
//    try
//    {
//        var bytes = @event.Body.ToArray();
//        string msg = Encoding.UTF8.GetString(bytes);
//        Console.WriteLine("Consumer2 收到了消息：" + msg);
//        channel.BasicAck(@event.DeliveryTag, multiple: false);
//        await Task.Delay(1000);
//    }
//    catch (Exception ex)
//    {
//        channel.BasicReject(@event.DeliveryTag, true);
//        Console.WriteLine("处理收到的消息出错" + ex);
//    }
//}

#endregion


#region Fanout 发布订阅模式

//// 1 创建信道 channel
//var channel = RabbitMQUtil.CreateChannel();

//var exchangeName = "logs";
//// 2 声明交换机
//channel.ExchangeDeclare(exchangeName, type: "fanout");

//// 3 声明临时队列
//var queueName = "fanout-Queue2";// Guid.NewGuid().ToString();
//channel.QueueDeclare(queueName, true, false, false, null);

//// 4 绑定队列到交换机
//channel.QueueBind(queueName, exchangeName, "");

//// 5 消费消息
//var consumer = new AsyncEventingBasicConsumer(channel);
//consumer.Received += ComsumerMessage;
//channel.BasicConsume(queueName, autoAck: false, consumer: consumer);
//Console.ReadLine();

//async Task ComsumerMessage(object sender, BasicDeliverEventArgs args)
//{
//    try
//    {
//        var bytes = args.Body.ToArray();
//        var msg = Encoding.UTF8.GetString(bytes);
//        Console.WriteLine("Consumer2 收到了消息：" + msg);
//        channel.BasicAck(args.DeliveryTag, multiple: false);
//        await Task.Delay(1000);
//    }
//    catch (Exception ex)
//    {
//        channel.BasicReject(args.DeliveryTag, true);
//        Console.WriteLine("处理消息异常：" + ex.Message);
//    }
//}
#endregion


//TestFanoutPattern.TestFanout();

//TestDirectPattern.TestDirect();

//TestTopicPattern.TestTopic();


// 测试预设值
//TestPrefetch.Test();


// 测试死信 TTL 过期
TestDeadQueue.TestTTL();

